package com.yumijinfu.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yumijinfu.R;
import com.yumijinfu.base.BaseActivity;
import com.yumijinfu.http.HttpCallBack;
import com.yumijinfu.http.HttpUtils;
import com.yumijinfu.mian.Config;
import com.yumijinfu.tool.Tool;
import com.yumijinfu.tool.VibratorUtil;
import com.yumijinfu.view.ClearEditText;
import com.yumijinfu.view.TitleBar;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 邮箱认证
 */
public class EmailRenZhengActivity extends BaseActivity {

    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.et_emils)
    ClearEditText etEmils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_email_ren_zheng);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        titlebar.setTitleBar(R.drawable.back_selector, "邮箱认证", 0);

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setData() {

    }

    @OnClick({R.id.titlebar_iv_back, R.id.tv_emils_renzheng})
    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.titlebar_iv_back:

                finish();

                break;

            case R.id.tv_emils_renzheng:
                long[] pattern = { 100, 400, 100, 400 };
                String emails = etEmils.getText().toString().trim();

                if (TextUtils.isEmpty(emails)) {

                    etEmils.setShakeAnimation();
                    VibratorUtil.Vibrate(EmailRenZhengActivity.this,pattern,false);
                    Toast.makeText(EmailRenZhengActivity.this, "请输入邮箱地址", Toast.LENGTH_SHORT).show();
                    return;

                } else if(!Tool.isEmail(emails)){
                    etEmils.setShakeAnimation();
                    Toast.makeText(EmailRenZhengActivity.this, "邮箱格式不正确", Toast.LENGTH_SHORT).show();


                }else{

                    verifyMailData();

                }

                break;
            default:
        }
    }

    /**
     *
     * 邮箱认证
     */
    private void verifyMailData() {
        HttpParams params=new HttpParams();
        params.put("userEmail",etEmils.getText().toString().trim());

        HttpUtils.post(Config.VERIFYMAIL, params, new HttpCallBack(this,"加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

                Toast.makeText(EmailRenZhengActivity.this, message+"请及时查看", Toast.LENGTH_SHORT).show();

                finish();
            }

            @Override
            public void onError(String code, String message) {

                Toast.makeText(EmailRenZhengActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });




    }
}
